package cn.kgc.springbootvuecontact.controller;

import cn.kgc.springbootvuecontact.ResultEntity;
import cn.kgc.springbootvuecontact.pojo.Contact;
import cn.kgc.springbootvuecontact.service.ContactService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ContactController {
    @Resource
    ContactService service;

    @RequestMapping("/select/contact")
    public ResultEntity<PageInfo<Contact>> selectContact(@RequestParam(value ="cname",required = false,defaultValue = "")String cname,
                                                             @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                                             @RequestParam(value ="pageSize",required = false,defaultValue = "5")Integer pageSize){
        PageInfo<Contact> pageInfo=service.getSelectBycname(cname, pageNum, pageSize);
        return ResultEntity.successWithoutData(pageInfo);
    }

    @RequestMapping("/select/contact/by/id")
    public ResultEntity<Contact> selectcontactByid(Integer id){
        Contact contact=service.getSelectByid(id);
        return ResultEntity.successWithoutData(contact);
    }
    @RequestMapping("/savecontact")
    public ResultEntity<Integer> savecontact(@RequestBody Contact contact){
        if(contact.getId()!=null){//修改
            System.out.println("修改");
            Integer integer=service.Update(contact);
            return ResultEntity.successWithoutData(integer);
        }else{//添加
            System.out.println("添加");
            Integer integer=service.Add(contact);
            return ResultEntity.successWithoutData(integer);
        }
    }
}
